using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniAspNetCore
{
    public static partial class Extentions
    {
        /// <summary>
        /// 获取处理程序
        /// </summary>
        /// <typeparam name="T">处理程序类型</typeparam>
        /// <param name="features">处理程序集合</param>
        /// <returns>处理程序实例</returns>
        public static T Get<T>(this IFeatureCollection features)
        {
            return features.TryGetValue(typeof(T), out object value) && value is T tValue ? tValue : default(T);
        }

        /// <summary>
        /// 注册处理程序
        /// </summary>
        /// <typeparam name="T">处理程序类型</typeparam>
        /// <param name="features">处理程序集合</param>
        /// <param name="feature">处理程序实例</param>
        /// <returns>处理程序集合</returns>
        public static IFeatureCollection Set<T>(this IFeatureCollection features, T feature)
        {
            features[typeof(T)] = feature;
            return features;
        }
    }
}
